use std::any::Any;
use log::info;

trait MyTrait {
    fn do_something(&self);
}

// 创建一个扩展 trait
trait AnyMyTrait: Any {
    fn as_my_trait(&self) -> Option<&dyn MyTrait>;
}

// 为所有实现了 MyTrait 和 Any 的类型实现 AnyMyTrait
impl<T: MyTrait + Any> AnyMyTrait for T {
    fn as_my_trait(&self) -> Option<&dyn MyTrait> {
        Some(self)
    }
}

struct MyStruct;
impl MyTrait for MyStruct {
    fn do_something(&self) {
        info!("MyStruct doing something");
    }
}

#[test]
fn main() {
    let my_struct = MyStruct;
    let any: &dyn Any = &my_struct;

    // 尝试转换为 AnyMyTrait
    if let Some(any_my_trait) = any.downcast_ref::<&dyn AnyMyTrait>() {
        if let Some(my_trait) = any_my_trait.as_my_trait() {
            my_trait.do_something();
        }
    }
}